package com.yunji.cabinet.dao.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunji.cabinet.dao.IDeviceEventDao;
import com.yunji.cabinet.domain.DeviceEventDO;
import com.yunji.cabinet.mapper.DeviceMapper;
import com.yunji.cabinet.params.DeviceEventParam;
import com.yunji.common.annotation.DataScope;
import com.yunji.common.base.BaseDaoImpl;
import com.yunji.common.utils.StringUtils;
import com.yunji.common.utils.bean.BeanUtils;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class DeviceEventDaoImpl extends BaseDaoImpl<DeviceMapper, DeviceEventDO> implements IDeviceEventDao {

    @Resource
    DeviceMapper cabinetRecordsMapper;

    @DataScope
    @Override
    public List<DeviceEventDO> search(DeviceEventParam param) {
        return pageList(param, new LambdaQueryWrapper<DeviceEventDO>()
                .eq(StringUtils.isNotEmpty(param.getUserType()), DeviceEventDO::getUserType, param.getUserType())
                .eq(StringUtils.isNotEmpty(param.getEventType()), DeviceEventDO::getEventType, param.getEventType())
                .eq(StringUtils.isNotEmpty(param.getEventCmd()), DeviceEventDO::getEventCmd, param.getEventCmd())
                .eq(StringUtils.isNotEmpty(param.getEventDataCode()), DeviceEventDO::getEventDataCode, param.getEventDataCode())
                .eq(StringUtils.isNotEmpty(param.getImei()), DeviceEventDO::getImei, param.getImei())
                .like(StringUtils.isNotEmpty(param.getSiteName()), DeviceEventDO::getSiteName, param.getSiteName())
                .like(StringUtils.isNotEmpty(param.getLocationName()), DeviceEventDO::getLocationName, param.getLocationName())
                .like(StringUtils.isNotEmpty(param.getRealName()), DeviceEventDO::getRealName, param.getRealName())
                //.like(StringUtils.isNotEmpty(param.getProductName()), DeviceEventDO::getProductName, param.getProductName())
                .like(StringUtils.isNotEmpty(param.getContainerName()), DeviceEventDO::getContainerName, param.getContainerName())
                .orderByDesc(DeviceEventDO::getCreateTime));
    }

    @Override
    public List<String> searchTableField(DeviceEventParam param) {
        return cabinetRecordsMapper.searchTableField(param);
    }

    @Override
    public DeviceEventDO getByEventDataCode(String eventDataCode, Integer finishStatus) {
        return lambdaQuery().eq(DeviceEventDO::getEventDataCode, eventDataCode)
                .eq(finishStatus != null, DeviceEventDO::getFinishStatus, finishStatus)
                .one();
    }

    @Override
    public List<DeviceEventDO> listByEventDataCode(List<String> eventDataCodeList) {
        return lambdaQuery().in(BeanUtils.isNotNull(eventDataCodeList),DeviceEventDO::getEventDataCode, eventDataCodeList).list();
    }

    @Override
    public void updateEvent(String maintainCode, String afterCmd) {
        lambdaUpdate().set(DeviceEventDO::getAfterCmd,afterCmd).eq(DeviceEventDO::getEventDataCode,maintainCode);
    }
}
